package com.smartsystem.common.config;

import com.smartsystem.common.security.JwtAuthenticationFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

/**
 * Spring Security配置类
 */
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    
    @Autowired
    private JwtAuthenticationFilter jwtAuthenticationFilter;
    
    /**
     * 密码编码器
     * @return 密码编码器
     */
    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
    
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            // 关闭CSRF
            .csrf().disable()
            // 不使用Session
            .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
            .and()
            // 配置URL访问权限
            .authorizeRequests()
            // 允许根路径访问
            .antMatchers("/", "/index.html").permitAll()
            // 允许API首页访问
            .antMatchers("/api").permitAll()
            // 允许认证接口匿名访问
            .antMatchers("/api/v1/auth/**").permitAll()
            // 允许测试接口访问
            .antMatchers("/api/test/**").permitAll()
            // 系统管理接口需要认证和相应权限
            .antMatchers("/api/v1/system/**").hasRole("ADMIN")
            .antMatchers("/api/system/**").hasRole("ADMIN")
            // 允许Knife4j文档访问
            .antMatchers("/doc.html", "/doc.html/**").permitAll()
            .antMatchers("/webjars/**").permitAll()
            .antMatchers("/v3/api-docs/**").permitAll()
            .antMatchers("/swagger-ui/**").permitAll()
            .antMatchers("/swagger-ui.html").permitAll()
            .antMatchers("/swagger-resources/**").permitAll()
            .antMatchers("/favicon.ico").permitAll()
            // 允许Druid监控访问
            .antMatchers("/druid/**").permitAll()
            // 允许健康检查
            .antMatchers("/actuator/**").permitAll()
            // 允许静态资源访问
            .antMatchers("/static/**", "/css/**", "/js/**", "/images/**").permitAll()
            // 允许错误页面访问
            .antMatchers("/error").permitAll()
            // 所有其他请求需要认证
            .anyRequest().authenticated();
        
        // 添加JWT过滤器
        http.addFilterBefore(jwtAuthenticationFilter, UsernamePasswordAuthenticationFilter.class);
    }
}